$(function () {
    init_message();
    edit_username();
    back_to_front();
    check_record();
    received();
});

function init_message() {
    $.ajax({
        url: '/user-self-info',
        type: 'get',
        dataType: 'json',
        success: function (data) {
            if (data.status) {
                // console.log(data.result);
                $('#username').text(data.result['name']);
                // $('#shopcount').text(data.result[0]['shopcount']);
            } else {
                alert('数据获取失败');
            }
        }
    });
}

function edit_username () {
    $('#edit-button').click(() => {
        if($('.edit-username-area').css('display') == 'none') {
            $('.edit-username-area').attr('style', 'display: inline-block;');
            $('#edit-confirm').attr('class', 'btn btn-success');
            $('#edit-confirm').text('确认');
            $('#edit-username').attr('class', 'form-control');
            $('#edit-username').val('');
            $('#edit-button').text('取消修改');
        } else {
            $('.edit-username-area').attr('style', 'display: none;');
            $('#edit-button').text('修改用户信息');
        }
    });
    $('#edit-confirm').click(() => {
        if($('#edit-username').val() != '') {
            $.ajax({
                url: '/user-edit-self',
                type: 'post',
                dataType: 'json',
                data: {
                    name: $('#edit-username').val(),
                },
                success: function (data) {
                    if (data.status) {
                        location.reload();
                    } else {
                        alert('数据提交失败');
                    }
                }
            });
        } else {
            $('#edit-username').attr('class', 'form-control change');
            $('#edit-confirm').attr('class', 'btn btn-danger');
            $('.edit-username-area').attr('style', 'width: 227; display: inline-block;');
            $('#edit-username').attr('style', 'width: 120px;');
            $('#edit.confirm').attr('style', 'width: 77px');
            $('#edit-confirm').text('无效用户名');
        }
    });
}

function back_to_front () {
    $('#back-button').click(() => {
        window.location.href = '/page/user-main.html';
    });
}

function check_record() {
    $('#check-record-button').click(() => {
        $('#check-record-button').attr('class', 'btn btn-success');
        $('#check-record-button').text('正在跳转..');
        setTimeout("window.location.href='/page/user-shopping-record.html';", 1500);
    });
}

function received() {
    $('#received-button').click(() => {
        $('#received-button').attr('class', 'btn btn-success');
        $('#received-button').text('正在跳转..');
        setTimeout("window.location.href='/page/user-order-received.html';", 1500);
    })
}